package top.viya.hello2.redis.scan;

import jakarta.annotation.Resource;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.stereotype.Component;
import top.viya.hello2.redis.common.RedisUtils;

@Component
public class ScanKeysWithSizeApplicationRunner implements ApplicationRunner {

    @Resource(name = "stringRedisTemplate")
    private RedisTemplate<String, String> stringRedisTemplate;

    @Resource(name = "redisUtils")
    private RedisUtils redisUtils;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        String keyPattern = "*";
        Integer batchCount = 100;
        ScanOptions options = ScanOptions.scanOptions().match(keyPattern).count(batchCount).build();
        Cursor<String> cursor = stringRedisTemplate.scan(options);
        while (cursor.hasNext()) {
            String keyName = cursor.next();
            Long size = redisUtils.getKeyMemorySize(keyName);
            System.out.println(keyName + "---------" + size);

        }

    }
}
